package org.jaaksi.kottlindemo.`object`

import kotlin.properties.Delegates

class User {
  // fixme 可观察属性 Observable
  var name: String by Delegates.observable("") { property, oldValue, newValue ->
    println("$oldValue -> $newValue")
    onChanged(oldValue, newValue)
  }

  fun onChanged(oldValue: String, newValue: String) {
    println("$oldValue -> $newValue")
  }

//  var age:Int by Delegates.vetoable(null){
//    property, oldValue, newValue ->
//    println("$oldValue -> $newValue")
//  }
}

class User2(val map: Map<String, Any?>) {
  val name: String by map
  val age: Int by map
}

fun test() {
  val user = User2(
    mapOf(
      "name" to "John Doe",
      "age" to 25
    )
  )

  // 委托属性会从这个映射中取值（通过字符串键——属性的名称）
  println(user.name) // Prints "John Doe"
  println(user.age)  // Prints 25
}

